#include <OgreRenderWindow.h>
#include <OgreLogManager.h>

#include "StateManager.h"
#include "InputManager.h"
#include "Core.h"


using namespace pong;

InputManager::~InputManager(void) {
	//Destroy the input devices
	if (inputMan) {
		if (mouse) {
			inputMan->destroyInputObject(mouse);
		}
		if (keys) {
			inputMan->destroyInputObject(keys);
		}
		OIS::InputManager::destroyInputSystem(inputMan);
	}
}

void InputManager::init(Ogre::RenderWindow* const renderWin) {
    Ogre::LogManager& logMan(Ogre::LogManager::getSingleton());

	size_t hwnd;
    //OIS::ParamList pl;
	renderWin->getCustomAttribute("WINDOW", &hwnd);
    //pl.insert(std::make_pair(std::string("WINDOW"), (std::ostringstream() << hwnd).str()));
	inputMan = OIS::InputManager::createInputSystem(hwnd/*pl*/);

    //Now, make the mouse and keyboard...
	keys = static_cast<OIS::Keyboard*>(inputMan->createInputObject(OIS::OISKeyboard, true));
	logMan.logMessage("Keyboard device created.");
	logMan.logMessage(Ogre::String("--Is ") + ((keys->buffered()) ? "buffered" : "unbuffered"));

	mouse = static_cast<OIS::Mouse*>(inputMan->createInputObject(OIS::OISMouse, true));
	unsigned int  width, height, depth;
	int left, top;
	renderWin->getMetrics(width, height, depth, left, top);
	
    //Set mouse region
	const OIS::MouseState& mouseState = mouse->getMouseState();
	mouseState.height = height;
	mouseState.width = width;

	logMan.logMessage("Mouse device created.");
	logMan.logMessage(Ogre::String("--Is ") + ((mouse->buffered()) ? "buffered" : "unbuffered"));
	
    logMan.logMessage("OIS Initialized successfully");

    mouse->setEventCallback(this);
    keys->setEventCallback(this);
}

void InputManager::capture(void) {
    mouse->capture();
    keys->capture();
}